package com.tau.egonetwork.gui.core.graphs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;

import com.tau.egonetwork.common.Log;
import com.tau.egonetwork.gui.Environment;
import com.tau.egonetwork.profiles.MainProfile;
import com.tau.egonetwork.profiles.Profile;

public class GraphNodeDetailDialog<T extends Comparable<T>> extends Dialog {

	private Shell shell;
	
	private MainProfile<T> profile;
	private T id;
	
	//can be calculated but easier to extract from graph:
	private int degree;
	private String groupName;
	
	public GraphNodeDetailDialog(Shell parent, MainProfile<T> profile, T id,
			int degree, String groupName) {
		super(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		this.profile = profile;
		this.id = id;
		this.degree = degree;
		this.groupName = groupName;
	}

	public void open() {
		
		Log.writeLineToLog("GraphNodeDetailDialog open() called for '" + getText() + "'");
		
		shell = new Shell(getParent(), getStyle());
		shell.setText(profile.getID().equals(id)
				? profile.getName()
				: profile.getFriendsList().get(id).getName());
		
		Log.writeLineToLog("createBaseWindowContents()");

		createWindowContents();
		
		shell.pack();
		
		//set shell at the center of display:
	    Monitor primary = getParent().getMonitor();
	    Rectangle bounds = primary.getBounds();
	    Rectangle rect = shell.getBounds();
	    int x = bounds.x + (bounds.width - rect.width) / 2;
	    int y = bounds.y + (bounds.height - rect.height) / 2;
	    shell.setLocation(x, y);
		
		Log.writeLineToLog("opening dialog");

		shell.open();
		
		//keep shell open, waiting for user input:
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	private void createWindowContents() {
		
		Profile<T> p = profile.getID().equals(id)
						? profile
						: profile.getFriendsList().get(id);

		shell.setLayout(new GridLayout(1, true));
		shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		
		Composite dialogComposite = new Composite(shell, SWT.NONE);
		dialogComposite.setLayout(new GridLayout(1, true));
		GridData dialogCompositeGridData = new GridData(SWT.FILL, SWT.FILL, true, true);
		if (!profile.getAnalyzedDataExists())
			dialogCompositeGridData.widthHint = 300;
		dialogComposite.setLayoutData(dialogCompositeGridData);
		
		Label nameLabel = new Label(dialogComposite, SWT.NONE);
		nameLabel.setText(p.getName());
		nameLabel.setFont(new Font(shell.getDisplay(), "Ariel", 14, SWT.BOLD));
		nameLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
		
		{
			
			Composite separatorComposite = new Composite(dialogComposite, SWT.NONE);
			GridLayout separatorCompositeGridLayout = new GridLayout(1, true);
			separatorCompositeGridLayout.marginWidth = 0;
			separatorCompositeGridLayout.marginLeft = 4;
			separatorCompositeGridLayout.marginRight = 4;
			separatorComposite.setLayout(separatorCompositeGridLayout);
			separatorComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
			
			Label separator = new Label(separatorComposite, SWT.SEPARATOR | SWT.HORIZONTAL);
			GridData separatorGridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
			separatorGridData.horizontalSpan = 2;
			separator.setLayoutData(separatorGridData);
			
		}
		
		Composite nodeDetailsComposite = new Composite(dialogComposite, SWT.NONE);
		nodeDetailsComposite.setLayout(new GridLayout(2, false));
		nodeDetailsComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		new Label(nodeDetailsComposite, SWT.NONE).setText("Degree: ");
		new Label(nodeDetailsComposite, SWT.NONE).setText(degree + "");

		if (profile.getAnalyzedDataExists()) {

			new Label(nodeDetailsComposite, SWT.NONE).setText("Clustering Coeffient: ");
			new Label(nodeDetailsComposite, SWT.NONE).setText(profile.getClusteringCoeffients().get(id) + "");

			if (!profile.getID().equals(id)) {
				//egocenteric node doesn't have this information
				new Label(nodeDetailsComposite, SWT.NONE).setText("Clustering Coeffient (without egocenteric): ");
				new Label(nodeDetailsComposite, SWT.NONE).setText(profile.getClusteringCoeffientsWithoutRoot().get(id) + "");
			}

			new Label(nodeDetailsComposite, SWT.NONE).setText("K-Shell Decomposition: ");
			new Label(nodeDetailsComposite, SWT.NONE).setText(profile.getkShellDecomposition().get(id) + "");

			if (!profile.getID().equals(id)) {
				//egocenteric node doesn't have this information
				new Label(nodeDetailsComposite, SWT.NONE).setText("K-Shell Decomposition (without egocenteric): ");
				new Label(nodeDetailsComposite, SWT.NONE).setText(profile.getkShellDecompositionWithoutRoot().get(id) + "");
			}

			new Label(nodeDetailsComposite, SWT.NONE).setText("Mean Shortest Paths: ");
			new Label(nodeDetailsComposite, SWT.NONE).setText(profile.getMeanShortestPaths().get(id) + "");

		}
		
		if (!profile.getID().equals(id)) {			
			new Label(nodeDetailsComposite, SWT.NONE).setText("Group: ");
			new Label(nodeDetailsComposite, SWT.NONE).setText(groupName);			
		}
		
		Button okButton = new Button(dialogComposite, SWT.PUSH);
		okButton.setText("OK");
		okButton.setImage(Environment.ICON_OK);
		okButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
		okButton.addListener(SWT.Selection, new Listener() {
			
			@Override
			public void handleEvent(Event event) {
				shell.dispose();
			}
		});

	}
}
